package com.shujia.flink.sql

import org.apache.flink.streaming.api.scala._
import org.apache.flink.table.api._
import org.apache.flink.table.api.bridge.scala._

object Demo5FileSystem {
  def main(args: Array[String]): Unit = {


    val bsEnv: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    val bsSettings: EnvironmentSettings = EnvironmentSettings
      .newInstance()
      .useBlinkPlanner() //使用blink计划器
      .inStreamingMode() //使用流模型
      .build()

    //创建flink sql的执行环境
    val bsTableEnv: StreamTableEnvironment = StreamTableEnvironment.create(bsEnv, bsSettings)

    /**
      * 读取文件构建表 --  有界流
      *
      */
    bsTableEnv.executeSql(
      """
        |CREATE TABLE student (
        |  id STRING,
        |  name STRING,
        |  age DOUBLE,
        |  gender STRING,
        |  clazz STRING
        |) WITH (
        |  'connector' = 'filesystem',
        |  'path' = 'data/students.txt',
        |  'format' = 'csv',
        |  'csv.field-delimiter' = ','
        |)
        |
      """.stripMargin)


    bsTableEnv.executeSql(
      """
        |CREATE TABLE print_table
        |(
        |clazz STRING,
        |avgAge DOUBLE
        |)
        |WITH ('connector' = 'print')
        |
      """.stripMargin)

    bsTableEnv.executeSql(
      """
        |
        |insert into print_table
        |select clazz,avg(age) avgAge from student group by clazz
        |
      """.stripMargin)


  }

}
